package com.ym.student.system.controller;

import com.ym.student.system.entity.StudentCourse;
import com.ym.student.system.service.IStudentCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/student-courses")
public class StudentCourseController {

    @Autowired
    private IStudentCourseService studentCourseService;

    // 获取所有学生选课记录
    @GetMapping
    public List<StudentCourse> getAllStudentCourses() {
        return studentCourseService.list();
    }

    // 根据ID获取单个记录
    @GetMapping("/{id}")
    public StudentCourse getStudentCourseById(@PathVariable Integer id) {
        return studentCourseService.getById(id);
    }

    // 添加学生选课记录
    @PostMapping
    public boolean addStudentCourse(@RequestBody StudentCourse studentCourse) {
        return studentCourseService.save(studentCourse);
    }

    // 更新成绩
    @PutMapping("/{id}")
    public boolean updateStudentCourse(@PathVariable Integer id, @RequestBody StudentCourse studentCourse) {
        studentCourse.setId(id); // 确保更新的是同一ID
        return studentCourseService.updateById(studentCourse);
    }

    // 删除记录
    @DeleteMapping("/{id}")
    public boolean deleteStudentCourse(@PathVariable Integer id) {
        return studentCourseService.removeById(id);
    }
}